package proxy_;

import org.junit.Test;

import java.lang.reflect.Proxy;

public class ProxyTest {


    public static void main(String[] args) {
        UserDao userDao = new UserDaoImpl();
        userDao.add();

        String result = userDao.query(6666);
        System.out.println(result);
    }

    @Test
    public void test1() {

        UserDao userDao = new UserDaoImpl();
        userDao.add();
        String result = userDao.query(123);
        System.out.println(result);

        System.out.println("-------------------");

        UserDao userDaoProxy = (UserDao) getUserDaoProxy(userDao);

        userDaoProxy.add();
        System.out.println(userDaoProxy.query(888));
    }


    private Object getUserDaoProxy(Object object) {
        MyInvocationHandler handler = new MyInvocationHandler(object);
        // 创建一个代理的对象, 要先明确代理的是哪一个对象
        // public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h)
        Object proxy = Proxy.newProxyInstance(
                object.getClass().getClassLoader(),
                object.getClass().getInterfaces(),// 获取当前类的接口
                handler);
        return proxy;
    }
}
